import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Vector;

/**
 * 连接池
 * 加载配置初始化
 * 加载数据库驱动
 * 对管道存储
 * @author ml
 * @create 2017-11-07--11:19
 */
public class MyDefaultPool implements IMyPool {

    private Vector<MyPooledConnection> myPooledConnectionVector=new Vector<MyPooledConnection>();

    private static String jdbcURL;
    private static String jdbcUserName;
    private static String jdbcPassword;
    private static int initCount;
    private static int step;
    private static int maxCount;

    public MyDefaultPool(){
        //初始化数据库连接池配置
        init();
        //加载驱动
        try{
            Class.forName(DBConfigXml.JDBC_DRIVER);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        createMyPooledConnection(initCount);
    }
    @Override
    public MyPooledConnection getMyPooledConnection() {

        if(myPooledConnectionVector.size()<1){
            throw new RuntimeException("连接池初始化错误");
        }
        MyPooledConnection myPooledConnection=null;
        try{
            myPooledConnection=getRealConnectFromPool();
            //连接池中已经没有可用的连接了
            if (myPooledConnection==null){
                createMyPooledConnection(step);
                myPooledConnection=getRealConnectFromPool();
                return myPooledConnection;
            }

        }catch (SQLException e){
            e.printStackTrace();
        }
        return myPooledConnection;
    }

    private synchronized MyPooledConnection getRealConnectFromPool() throws SQLException {
        for(MyPooledConnection myPooledConnection:myPooledConnectionVector){
            if(!myPooledConnection.isBusy()){
                if(myPooledConnection.getConnection().isValid(3000)){
                    myPooledConnection.setBusy(true);
                    return myPooledConnection;
                }else {
                    //得到的管道已超时
                    Connection connection=DriverManager.getConnection(jdbcURL,jdbcUserName,jdbcPassword);
                    myPooledConnection.setConnection(connection);
                    myPooledConnection.setBusy(true);
                    return myPooledConnection;
                }
            }
        }
        return null;
    }

    @Override
    public void createMyPooledConnection(int count) {

        if(myPooledConnectionVector.size()>maxCount ||
                myPooledConnectionVector.size()+count>maxCount){
            throw new RuntimeException("连接池已满");

        }

        for(int i=0;i<count;i++){
            try{
                Connection connection= DriverManager.getConnection(jdbcURL,jdbcUserName,jdbcPassword);
                MyPooledConnection myPooledConnection=new MyPooledConnection(connection,false);
                myPooledConnectionVector.add(myPooledConnection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    private void init(){
        jdbcURL=DBConfigXml.JDBC_URL;
        jdbcUserName=DBConfigXml.JDBC_USERNAME;
        jdbcPassword=DBConfigXml.JDBC_PASSWORD;
        initCount=DBConfigXml.INIT_COUNT;
        step=DBConfigXml.STEP;
        maxCount=DBConfigXml.MAX_COUNT;
    }

}
